package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@RestController
public class CardCountController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    private static final Logger logger = LoggerFactory.getLogger(CardCountController.class);

   /* @PostMapping("/youmi_http")
    @ResponseBody
    public ResObj test(CardCount cardCount){
        logger.info("deviceId:"+cardCount.getDeviceId()+"cardId:"+cardCount.getCardID()+"_starttime"+cardCount.getStart()+"_endtime"+cardCount.getEnd()+"sendTime:"+System.currentTimeMillis()/1000);
        ValueOperations  valueOperations = redisTemplate.opsForValue();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        ResObj res = new ResObj();
        ReturnRes re = new ReturnRes();

        String[]  strStart = cardCount.getStart().split(":");
        String[]  strEnd = cardCount.getEnd().split(":");
        String starthour = strStart[0];
        String  startmin = strStart[1];
       int start = Integer.valueOf(starthour)*60+ Integer.valueOf(startmin);
        String endhour = strEnd[0];
        String  endmin = strEnd[1];
        int end = Integer.valueOf(endhour)*60+ Integer.valueOf(endmin);
        int t = end-start;
        try {
            CardCount  cardCount1= (CardCount)valueOperations.get(cardCount.getCardID());
            if(cardCount1==null){
                cardCount.setCardCount(1);
                valueOperations.set(cardCount.getCardID(),cardCount,t,TimeUnit.MINUTES);
                re.setCOLNUM(cardCount.getCardCount());
            }else{
                cardCount.setCardCount(cardCount1.getCardCount()+1);
                valueOperations.set(cardCount.getCardID(),cardCount,t,TimeUnit.MINUTES);
                re.setCOLNUM(cardCount.getCardCount());
            }
            res.setReturnCode("000");
            res.setInsertKqInfo(re);
        } catch (Exception e) {
            res.setReturnCode("000");
            re.setCOLNUM(1);
            res.setInsertKqInfo(re);
        }
        logger.info("deviceId:"+cardCount.getDeviceId()+"cardId:"+cardCount.getCardID()+"_starttime"+simpleDateFormat.format(new Date())+"_"+cardCount.getStart()+"_endtime"+cardCount.getEnd()+"_"+cardCount.getCardCount()+"returnTime"+System.currentTimeMillis()/1000);
        return res;

    }*/
   @PostMapping("/youmi_http")
   @ResponseBody
    public ResObj test1(CardCount cardCount) {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
       ResObj res =  new ResObj();
       ReturnRes re = new ReturnRes();
       try {
          // logger.info("deviceId:"+cardCount.getDeviceId()+"cardId:"+cardCount.getCardID()+"_starttime"+cardCount.getStart()+"_endtime"+cardCount.getEnd()+"sendTime:"+System.currentTimeMillis()/1000);
           String[]  strStart = cardCount.getStart().split(":");
            String[]  strEnd = cardCount.getEnd().split(":");
            String starthour = strStart[0];
            String  startmin = strStart[1];
            int start = Integer.valueOf(starthour)*60+ Integer.valueOf(startmin);
            String endhour = strEnd[0];
            String  endmin = strEnd[1];
            int end = Integer.valueOf(endhour)*60+ Integer.valueOf(endmin);
            int t = end-start;
            ValueOperations<String,Integer> valueOperations=  redisTemplate.opsForValue();

           if(Integer.valueOf(starthour)>=18){
               Integer   count=  valueOperations.get(cardCount.getCardID()+"_"+"18:00");
                if(count==null||"".equals(count)){
                   cardCount.setCardCount(1);
                   valueOperations.set(cardCount.getCardID()+"_"+"18:00",1,t,TimeUnit.MINUTES);
                   re.setCOLNUM(1);
               }else{
                   re.setCOLNUM( valueOperations.increment(cardCount.getCardID()+"_"+"18:00").intValue());
               }
           }else{
               Integer count =  valueOperations.get(cardCount.getCardID());
               if(count==null||"".equals(count)){
                   cardCount.setCardCount(1);
                   valueOperations.set(cardCount.getCardID(),1,t,TimeUnit.MINUTES);
                   re.setCOLNUM(1);
               }else{
                   re.setCOLNUM( valueOperations.increment(cardCount.getCardID()).intValue());
               }

           }
            res.setReturnCode("000");
            res.setInsertKqInfo(re);
          // logger.info("deviceId:"+cardCount.getDeviceId()+"cardId:"+cardCount.getCardID()+"_starttime"+simpleDateFormat.format(new Date())+"_"+cardCount.getStart()+"_endtime"+cardCount.getEnd()+"_"+cardCount.getCardCount()+"_returnTime"+System.currentTimeMillis()/1000);
       } catch (Exception e) {
           res.setReturnCode("000");
           re.setCOLNUM(1);
           res.setInsertKqInfo(re);
           e.printStackTrace();
       }
        return res;
    }

}
